package com.kingyea.app.market.supplierAllocation.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyea.app.market.client.UserServiceClient;
import com.kingyea.app.market.supplierAllocation.bean.SupplierAllocation;
import com.kingyea.app.market.supplierAllocation.service.SupplierAllocationService;
import com.kingyea.app.market.utils.page.PageInfo;
import com.kingyea.mobilepolice.user.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version v1.0.0
 * @description SupplierAllocation 控制层
 * @date Created in 2018-02-09 17:48:58
 */
@RequestMapping(value = "/supplierAllocation")
@RestController
public class SupplierAllocationController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierAllocationController.class);

    @Autowired
    private SupplierAllocationService supplierAllocationService;
    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(@RequestBody Map<String, Object> supplierAllocation) throws Exception {
        return supplierAllocationService.list(supplierAllocation);
    }

    /**
     * 根据供应商查询已分配用户
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSupplierUserList", method = RequestMethod.POST)
    public Object userList(@RequestBody Map<String, Object> map) throws Exception {
        List<String> supplierCodeList = new ArrayList<String>();
        List<Object> listNoPage = supplierAllocationService.listNoPage(map);
        for (Object obj : listNoPage) {
            JSONObject sup = JSON.parseObject(JSON.toJSONString(obj));
            if (sup.get("loginAccount") != null) {
                supplierCodeList.add(sup.get("loginAccount").toString());
            }
        }
        map.put("pageNo", map.get("pageNum"));
        map.put("userIdList", supplierCodeList);
        PageInfo p = new PageInfo();
        if (supplierCodeList.size() < 1) {
            p.setPageNum(Integer.parseInt(map.get("pageNum").toString()));
            p.setPageSize(Integer.parseInt(map.get("pageSize").toString()));
            p.setTotal(0);
            return p;
        }
        Object object = userServiceClient.getUserList(map);
        JSONObject data = JSON.parseObject(JSON.toJSONString(object));
        JSONObject results = JSON.parseObject(JSON.toJSONString(data.get("data")));
        List<User> userList = (List<User>) results.get("list");
        p.setList(userList);
        if (userList.size() > 0) {
            p.setPageNum(Integer.parseInt(results.get("pageNo").toString()) == 0 ? 1 : Integer.parseInt(results.get("pageNo").toString()));
            p.setPageSize(Integer.parseInt(results.get("pageSize").toString()));
            p.setTotal(Long.parseLong(results.get("totalRecord").toString()));
        } else {
            p.setPageNum(Integer.parseInt(map.get("pageNum").toString()));
            p.setPageSize(Integer.parseInt(map.get("pageSize").toString()));
            p.setTotal(userList.size());
        }
        return p;
    }

    /**
     * 根据供应商查询已分配市场
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSupplierMarketList", method = RequestMethod.POST)
    public Object marketList(@RequestBody Map<String, Object> supplierAllocation) throws Exception {
        return supplierAllocationService.getMarketList(supplierAllocation);
    }

    /**
     * 根据供应商查询相关联供应商
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSupplierSupplierList", method = RequestMethod.POST)
    public Object linkSupplierList(@RequestBody Map<String, Object> supplierAllocation) throws Exception {
        return supplierAllocationService.getLinkSupplier(supplierAllocation);
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public SupplierAllocation detail(@PathVariable Integer id) throws Exception {
        return supplierAllocationService.getById(id);
    }

    /**
     * 创建
     *
     * @return
     */
    @RequestMapping(value = "/distribution", method = RequestMethod.POST)
    public Object create(@RequestBody Map<String, Object> obj) throws Exception {
        List<Object> list = new ArrayList<Object>();
        SupplierAllocation supplier = new SupplierAllocation();
        String supplierCode = "";
        if (obj.get("supplierCode") != null && !"".equals(obj.get("supplierCode"))) {
            supplierCode = obj.get("supplierCode").toString();
        }
        //分配用户
        if (obj.get("userIdList") != null && !"".equals(obj.get("userIdList"))) {
            String[] _userIdList = obj.get("userIdList").toString().split(",");
            for (int i = 0; i < _userIdList.length; i++) {
                supplier = new SupplierAllocation();
                supplier.setSupplierCode(supplierCode);
                supplier.setLoginAccount(_userIdList[i]);
                list.add(supplier);
            }
        }
        //分配市场
        if (obj.get("marketCodes") != null && !"".equals(obj.get("marketCodes"))) {
            String[] _marketCodes = obj.get("marketCodes").toString().split(",");
            for (int i = 0; i < _marketCodes.length; i++) {
                supplier = new SupplierAllocation();
                supplier.setSupplierCode(supplierCode);
                supplier.setMarketCode(_marketCodes[i]);
                list.add(supplier);
            }
        }
        //分批关联供应商
        if (obj.get("supplierCodes") != null && !"".equals(obj.get("supplierCodes"))) {
            String[] _supplierCodes = obj.get("supplierCodes").toString().split(",");
            for (int i = 0; i < _supplierCodes.length; i++) {
                supplier = new SupplierAllocation();
                supplier.setSupplierCode(supplierCode);
                supplier.setLinkSupplier(_supplierCodes[i]);
                list.add(supplier);
            }
        }
        supplierAllocationService.save(list);
        return null;
    }

    /**
     * 更新
     *
     * @param supplierAllocation
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public SupplierAllocation update(@RequestBody SupplierAllocation supplierAllocation) throws Exception {
        return supplierAllocationService.update(supplierAllocation);
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping(value = "/deleteSupplierAllocation", method = RequestMethod.POST)
    public Object delete(@RequestBody SupplierAllocation supplierAllocation) throws Exception {
        return supplierAllocationService.deleteById(supplierAllocation.getId());
    }

    /**
     * 删除供应商下用户
     *
     * @return
     */
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public Object deleteUser(@RequestBody Map<String, Object> obj) {
        String supplierCode = obj.get("supplierCode").toString();
        String loginAccount = obj.get("id").toString();
        return supplierAllocationService.deleteUser(supplierCode, loginAccount);
    }

    /**
     * 删除供应商下市场
     *
     * @return
     */
    @RequestMapping(value = "/deleteMarket", method = RequestMethod.POST)
    public Object deleteMarket(@RequestBody Map<String, Object> obj) {
        String supplierCode = obj.get("supplierCode").toString();
        String marketCode = obj.get("marketCode").toString();
        return supplierAllocationService.deleteMarket(supplierCode, marketCode);
    }

    /**
     * 删除供应商下供应商
     *
     * @return
     */
    @RequestMapping(value = "/deleteSupplier", method = RequestMethod.POST)
    public Object deleteSupplier(@RequestBody Map<String, Object> obj) {
        String supplierCode = obj.get("supplierCode").toString();
        String linkSupplier = obj.get("linkSupplier").toString();
        return supplierAllocationService.deleteSupplier(supplierCode, linkSupplier);
    }

}
